package com.tarena.demo.luban.all.main.service.impl;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.tarena.demo.luban.all.main.component.CartRpcDeleteComponent;
import com.tarena.demo.luban.all.main.mapper.OrderMapper;
import com.tarena.demo.luban.all.main.service.OrderService;
import com.tarena.demo.luban.commons.exception.BusinessDemoException;
import com.tarena.demo.luban.commons.restful.ResponseCode;
import com.tarena.demo.luban.protocol.cart.param.CartDeleteParam;
import com.tarena.demo.luban.protocol.order.dos.OrderDO;
import com.tarena.demo.luban.protocol.order.param.OrderAddParam;
import com.tarena.demo.luban.protocol.stock.param.StockReduceCountParam;
import com.tarena.luban.demo.cart.api.CartRpcApi;
import com.tarena.luban.demo.stock.api.StockApi;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Autowired
    private CartRpcApi cartRpcApi;
    /*@Autowired
    private CartRpcDeleteComponent cartRpcApi;*/
    @Autowired
    private StockApi stockApi;
    @Autowired
    private OrderMapper orderMapper;
    public void blockHandle(OrderAddParam param, BlockException e){
        throw new BusinessDemoException(ResponseCode.BAD_REQUEST,"新增订单被限流拦截了");
        //System.out.println("新增订单被限制流量了");
    }
    public void fallback(OrderAddParam param,Throwable e){
        System.out.println("原方法执行出现异常");
    }
    /*@SentinelResource(
            value="orderAdd",
            blockHandler = "blockHandle",
            fallback = "fallback")*/
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @Override public void addOrder(OrderAddParam param) {
        //利用参数orderSn 查询订单,如果订单已经存在了
        String orderSn=param.getOrderSn();
        //select count(*) from order_tbl where order_sn=#{orderSn}
        Integer count=orderMapper.countOrderByOrderSn(orderSn);
        if (count>0){
            log.info("当前订单{},已经存在了",orderSn);
            return;
        }
        stockApi.reduceCount(param.getOrderSn(),param.getProductCode(),param.getCount());
        OrderDO orderDO=new OrderDO();
        BeanUtils.copyProperties(param,orderDO);
        orderMapper.insertOrder(orderDO);
        //发送消息
        Message message= MessageBuilder.withPayload(param).build();
        rocketMQTemplate.asyncSend("cart-delete-topic", message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {

            }

            @Override
            public void onException(Throwable throwable) {

            }
        });
        //cartRpcApi.cartDelete(param.getUserId(), param.getProductCode());
    }
}
